package app

import (
	"github.com/gin-gonic/gin"
	"mallgo/internal/common/response"
	"mallgo/internal/resources/app/api/domain/request"
	"mallgo/internal/resources/app/resource"
	"net/http"
	"strconv"
)

type AppMenuController struct {
	appMenuResource *resource.AppMenuResource // 假设你有一个 AppMenuResource 接口或结构体
}

func NewAppMenuController(appMenuResource *resource.AppMenuResource) *AppMenuController {
	return &AppMenuController{appMenuResource: appMenuResource}
}

func (a *AppMenuController) Create(c *gin.Context) {
	var req request.AppMenuRequest
	if err := c.ShouldBindJSON(&req); err != nil {
		response.Error(400, "请求参数错误").ToJSON(c)
		return
	}
	c.JSON(http.StatusOK, a.appMenuResource.Create(&req))
}

func (a *AppMenuController) Get(c *gin.Context) {
	id, err := strconv.ParseInt(c.Param("id"), 10, 64)
	if err != nil {
		response.Error(400, "无效的ID").ToJSON(c)
		return
	}
	c.JSON(http.StatusOK, a.appMenuResource.Get(id))
}

func (a *AppMenuController) Update(c *gin.Context) {
	id, err := strconv.ParseInt(c.Param("id"), 10, 64)
	if err != nil {
		response.Error(400, "无效的ID").ToJSON(c)
		return
	}

	var req request.AppMenuRequest
	if err := c.ShouldBindJSON(&req); err != nil {
		response.Error(400, "请求参数错误").ToJSON(c)
		return
	}

	a.appMenuResource.Update(id, &req).ToJSON(c)
}

func (a *AppMenuController) Delete(c *gin.Context) {
	id, err := strconv.ParseInt(c.Param("id"), 10, 64)
	if err != nil {
		response.Error(400, "无效的ID").ToJSON(c)
		return
	}
	a.appMenuResource.Delete(id).ToJSON(c)
}
